package com.aleaho.cftrack.presenter;


import android.app.Notification;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.R;
import com.aleaho.cftrack.model.MainModel;
import com.aleaho.cftrack.model.MainModelImpl;
import com.aleaho.cftrack.utils.CommonUtil;
import com.aleaho.cftrack.utils.Constants;
import com.aleaho.cftrack.view.MainView;
import com.baidu.trace.api.fence.FenceAlarmPushInfo;
import com.baidu.trace.api.fence.MonitoredAction;
import com.baidu.trace.model.OnTraceListener;
import com.baidu.trace.model.PushMessage;
import com.baidu.trace.model.StatusCodes;


/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/10/28
 * @desc
 */

public class MainPresenter implements OnTraceListener {

    private MainModel mMainModel = null;

    private MainView mMainView = null;
    private int notifyId = 0;

    private CFTrackApplication trackApp = null;


    public MainPresenter(CFTrackApplication trackApp, MainView traceView) {
        this.mMainView = traceView;
        this.trackApp = trackApp;
        this.mMainModel = new MainModelImpl();
    }


    /**
     * 开启或关闭鹰眼服务
     */
    public void traceManager() {
        mMainModel.traceManager(trackApp, this);
    }


    /**
     * 开启鹰眼服务
     */
    public void startTrace() {
        mMainModel.startTrace(trackApp, this);
    }

    /**
     * 关闭鹰眼服务
     */
    public void stopTrace() {
        mMainModel.stopTrace(trackApp, this);
    }


    /**
     * 开启采集服务
     */
    public void startGather() {
        mMainModel.startGather(trackApp, this);
    }


    /**
     * 关闭采集服务
     */
    public void stopGather() {
        mMainModel.stopGather(trackApp, this);
    }


    /**
     * 开启或关闭采集服务
     */
    public void gatherManager() {
        mMainModel.gatherManager(trackApp, this);
    }


    /**
     * 绑定服务回调接口
     *
     * @param errorNo 状态码
     * @param message 消息
     *                <p>
     *                <pre>0:成功</pre>
     *                <pre>1:失败</pre>
     *                </p>
     */
    @Override
    public void onBindServiceCallback(int errorNo, String message) {

        String msg = "";
        msg = "正在绑定位置服务。";
        Log.i(Constants.TAG, String.format("鹰眼绑定服务信息：状态码:%d, 消息:%s ", errorNo, message));

        mMainView.showMessage(msg);
    }


    /**
     * 开启服务回调接口
     *
     * @param errorNo 状态码
     * @param message 消息
     *                <p>
     *                <pre>0：成功 </pre>
     *                <pre>10000：请求发送失败</pre>
     *                <pre>10001：服务开启失败</pre>
     *                <pre>10002：参数错误</pre>
     *                <pre>10003：网络连接失败</pre>
     *                <pre>10004：网络未开启</pre>
     *                <pre>10005：服务正在开启</pre>
     *                <pre>10006：服务已开启</pre>
     */
    @Override
    public void onStartTraceCallback(int errorNo, String message) {

        String msg = "";
        if (StatusCodes.SUCCESS == errorNo || StatusCodes.START_TRACE_NETWORK_CONNECT_FAILED <= errorNo) {

            trackApp.isTraceStarted = true;

            mMainView.setTraceStyle();
            mMainView.registerReceiver();

            startGather();

            msg = "开启位置信息服务成功。";
        } else {
            msg = "开启位置信息服务失败。";
        }

        Log.i(Constants.TAG, String.format("开启服务回调接口: 状态码:%d, 消息:%s ", errorNo, message));

        mMainView.showMessage(msg);
    }

    /**
     * 停止服务回调接口
     *
     * @param errorNo 状态码
     * @param message 消息
     *                <p>
     *                <pre>0：成功</pre>
     *                <pre>11000：请求发送失败</pre>
     *                <pre>11001：服务停止失败</pre>
     *                <pre>11002：服务未开启</pre>
     *                <pre>11003：服务正在停止</pre>
     */
    @Override
    public void onStopTraceCallback(int errorNo, String message) {
        String msg = "";
        if (StatusCodes.SUCCESS == errorNo || StatusCodes.CACHE_TRACK_NOT_UPLOAD == errorNo) {
            trackApp.isTraceStarted = false;
            trackApp.isGatherStarted = false;

            mMainView.setTraceStyle();
            mMainView.unregisterPowerReceiver();
            msg = "关闭位置信息服务成功。";
        } else {
            msg = "关闭位置信息服务失败。";
        }

        Log.i(Constants.TAG, String.format("停止服务回调接口: 状态码:%d, 消息:%s ", errorNo, message));

        mMainView.showMessage(msg);
    }

    /**
     * 开启采集回调接口
     *
     * @param errorNo 状态码
     * @param message 消息
     *                <p>
     *                <pre>0：成功</pre>
     *                <pre>12000：请求发送失败</pre>
     *                <pre>12001：采集开启失败</pre>
     *                <pre>12002：服务未开启</pre>
     *                <pre>12002：采集已开启</pre>
     */
    @Override
    public void onStartGatherCallback(int errorNo, String message) {

        String msg = "";
        if (StatusCodes.SUCCESS == errorNo || StatusCodes.GATHER_STARTED == errorNo) {
            trackApp.isGatherStarted = true;
            mMainView.setTraceStyle();
            msg = "开启采集位置信息服务成功。";
        } else {
            msg = "开启采集位置信息服务失败。";
        }

        Log.i(Constants.TAG, String.format("开启采集回调接口: 状态码:%d, 消息:%s ", errorNo, message));

        mMainView.showMessage(msg);

    }

    /**
     * 停止采集回调接口
     *
     * @param errorNo 状态码
     * @param message 消息
     *                <p>
     *                <pre>0：成功</pre>
     *                <pre>13000：请求发送失败</pre>
     *                <pre>13001：采集停止失败</pre>
     *                <pre>13002：服务未开启</pre>
     */
    @Override
    public void onStopGatherCallback(int errorNo, String message) {
        String msg = "";
        if (StatusCodes.SUCCESS == errorNo || StatusCodes.GATHER_STOPPED == errorNo) {
            trackApp.isGatherStarted = false;
            mMainView.setTraceStyle();

            msg = "关闭采集位置信息服务成功。";
        } else {
            msg = "关闭采集位置信息服务失败。";
        }
        //        viewUtil.showToast(LocationActivity.this,
        //                String.format("onStopGatherCallback, errorNo:%d, message:%s ", errorNo, message));

        Log.i(Constants.TAG, String.format("停止采集回调接口: 状态码:%d, 消息:%s ", errorNo, message));

        mMainView.showMessage(msg);
    }

    /**
     * 推送消息回调接口
     *
     * @param messageType 状态码
     * @param pushMessage 消息
     *                    <p>
     *                    <pre>0x01：配置下发</pre>
     *                    <pre>0x02：语音消息</pre>
     *                    <pre>0x03：服务端围栏报警消息</pre>
     *                    <pre>0x04：本地围栏报警消息</pre>
     *                    <pre>0x05~0x40：系统预留</pre>
     *                    <pre>0x41~0xFF：开发者自定义</pre>
     */
    @Override
    public void onPushCallback(byte messageType, PushMessage pushMessage) {
        if (messageType < 0x03 || messageType > 0x04) {
            mMainView.showMessage(pushMessage.getMessage());
            return;
        }
        FenceAlarmPushInfo alarmPushInfo = pushMessage.getFenceAlarmPushInfo();
        if (null == alarmPushInfo) {
            mMainView.showMessage(String.format("行程管理推送消息：状态码:%d, 信息:%s ", messageType,
                    pushMessage));
            return;
        }
        StringBuffer alarmInfo = new StringBuffer();
        alarmInfo.append("您于")
                .append(CommonUtil.getHMS(alarmPushInfo.getCurrentPoint().getLocTime() * 1000))
                .append(alarmPushInfo.getMonitoredAction() == MonitoredAction.enter ? "进入" : "离开")
                .append(messageType == 0x03 ? "云端" : "本地")
                .append("围栏：").append(alarmPushInfo.getFenceName());


        Log.i(Constants.TAG, alarmInfo.toString());

        //        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        Notification notification = new Notification.Builder(trackApp)
                .setContentTitle(((Context) trackApp).getResources().getString(R.string.alarm_push_title))
                .setContentText(alarmInfo.toString())
                .setSmallIcon(R.mipmap.icon)
                .setWhen(System.currentTimeMillis()).build();
        mMainView.notify(notifyId++, notification);
        //        }
    }

    @Override
    public void onInitBOSCallback(int errorNo, String message) {

    }
}
